package com.jinduplan.planmanager.ui.activity.user

import android.view.View
import com.cxz.kotlin.baselibs.base.BaseMvpFragment
import com.cxz.kotlin.baselibs.base.BaseMvpTitleActivity
import com.cxz.kotlin.baselibs.base.lubanCompress
import com.cxz.kotlin.baselibs.utils.AppUtils
import com.cxz.kotlin.baselibs.utils.GsonHelper
import com.cxz.kotlin.baselibs.utils.LogUtil
import com.cxz.kotlin.baselibs.utils.UserInfoUtil
import com.cxz.kotlin.baselibs.widget.PhotoSelectDialog
import com.huantansheng.easyphotos.callback.SelectCallback
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.constant.Constant
import com.jinduplan.planmanager.mvp.contract.PersonalContract
import com.jinduplan.planmanager.mvp.contract.TestContract
import com.jinduplan.planmanager.mvp.presenter.PersonalPresenter
import com.jinduplan.planmanager.mvp.presenter.TestPresenter
import com.jinduplan.planmanager.utils.GlideUtil
import com.jinduplan.planmanager.widgets.ComfirCancelDialog
import com.jinduplan.planmanager.widgets.InputDialog
import kotlinx.android.synthetic.main.activity_personal_info.*
import kotlinx.android.synthetic.main.fragment_mine.*

/**
 * @author chenxz
 * @date 2018/11/30
 * @desc
 */
class PersonalInfoActivity : BaseMvpTitleActivity<PersonalContract.View, PersonalContract.Presenter>(),
    PersonalContract.View {

    private val callback: SelectCallback = object : SelectCallback() {

        override fun onResult(photos: ArrayList<Photo>, isOriginal: Boolean) {
            LogUtil.d("Photo->${GsonHelper.toJson(photos)}")
            photos[0].path.lubanCompress(this@PersonalInfoActivity,{
                mPresenter?.unloadPics(it)
            })
        }

        override fun onCancel() {

        }
    }

    override fun createPresenter(): PersonalContract.Presenter = PersonalPresenter()

    override fun start() {
        setBaseTitle("我的信息")

        if (!UserInfoUtil.avatar.isEmpty()){
            GlideUtil.load(UserInfoUtil.avatar,ivHead)
        }
        if (UserInfoUtil.nick_name.isEmpty()) {
            tvName.text = UserInfoUtil.userName
        } else {
            tvName.text = UserInfoUtil.nick_name
        }
        tvJobTitle.text = UserInfoUtil.position
        tvPhone.text = UserInfoUtil.userName
        tvScore.text = UserInfoUtil.score
        tvLogout.setOnClickListener {
            ComfirCancelDialog(this).setContent("是否退出", "确认后将会退出您的当前账号", {
                UserInfoUtil.logout(this)
            }).show()
        }
        ivHead.setOnClickListener {
            PhotoSelectDialog(this, callback).show()
        }
        tvNameClick.setOnClickListener {
            InputDialog(this).setContent("请输入名称",{
                mPresenter?.modifyNickName(it)
            }).show()

        }
        tvChangePsw.setOnClickListener {

            ComfirCancelDialog(this).setContent("是否修改密码", "确认后将跳转到修改密码页面", {
                UserInfoUtil.goChangpsw(this)
            }).show()
        }

        tvChangePsw.setOnClickListener {
            goActivity(ChangePswAcitivity::class.java)
        }
        tvVersion.setText("V_${AppUtils.getVerName(this)}")

       val title =  when (UserInfoUtil.labour_type) {
            "1"->{
                "劳工"
            }
            "2"->{
                "班组长"
            }
           "3"->{
               "项目经理"
           }
           else->{
               ""
           }
        }
        tvJobTitle.setText(title)
    }


    override fun attachChildLayoutRes(): Int {
        return R.layout.activity_personal_info
    }

    override fun uploadPiccSuccess(url: String) {
        val pic = "${Constant.BASE_URL}/$url"
        LogUtil.d("pic->$pic")
        GlideUtil.load(pic, ivHead)

    }

    override fun modifyNIckNameSuccess(nickName: String) {
        tvName.setText(nickName)

    }
}